package com.neu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neu.base.Result;

import com.neu.entity.*;
import com.neu.service.CommonReportInfoService;
import com.neu.service.ProjectRskService;
import com.neu.service.ProjectService;
import com.neu.service.impl.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/costReport")
@Tag(name = "造价综合评估报告")
public class CostReportController {
    @Autowired
    private PDRServiceImpl pdrService;

    @Autowired
    private RDFServiceImpl rdfService;

    @Autowired
    private SWFServiceImpl swfService;

    @Autowired
    private ProjectRskService projectRskService;

    @Autowired
    private CostServiceImpl costService;

    @Autowired
    private ProjectService projectService;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private CommonReportInfoService reportInfoService;

    @Autowired
    private TaskServiceImpl taskService;

    @PostMapping("/getData")
    @Parameter(name = "id", description = "项目ID", required = true)
    @Operation(summary = "获取综合评估数据")
    public Result updateCost(@RequestParam String id){


        PDR pdr = pdrService.getById(id);
        RDF rdf = rdfService.getById(id);
        SWF swf = swfService.getById(id);
        ProjectRsk projectRsk = projectRskService.getById(id);
        Cost cost = costService.getById(id);
        Project project = projectService.getById(id);

        Map map = new HashMap<>();
        map.put("projectName", project.getProjectName());
        map.put("pdr",pdr);
        map.put("rdf",rdf);
        map.put("swf",swf);
        map.put("rsk",projectRsk);
        map.put("cost",cost);
        map.put("gscchoice",true);
        map.put("gsc", null);
        map.put("scale", null);


        return Result.success(map);
    }

    @PostMapping("/reportReceive")
    @Parameter(name = "id", description = "项目ID信息", required = true)
    @Operation(summary = "造价综合评估报告")
    public Result reportMethod(HttpServletRequest request, @RequestParam String id){

        Map<String,Object>requestBody = new HashMap<>();
        ReportData json = costService.getReportData(id);
        requestBody.put("result", json);
        String flaskUrl = "http://localhost:5000/receive";
        String token =request.getHeader("Authorization");
        requestBody.put("token", token);
        ResponseEntity<Result> response = restTemplate.postForEntity(flaskUrl, requestBody, Result.class);
        Result<Map> body = response.getBody();
        Map<String,String> map = (Map) body.getData();
        System.out.println("1\n\n\n");
        /**
         * 更新项目状态
         */
        Project project = projectService.getById(id);
        project.setUpdateTime(LocalDateTime.now());
        projectService.updateById(project);

        /**
         * 更新任务状态
         */
        QueryWrapper<Task> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("project_id", id)           // 等于条件
                .eq("task_type",2);        // IN 条件

        Task task = taskService.getOne(queryWrapper);
        task.setTaskStatus("2");
        taskService.updateById(task);

        /**
         * 更新报告信息
         */
        ReportInfo reportInfo = new ReportInfo();
        reportInfo.setAiId((String)map.get("aiId"));
        reportInfo.setUrl((String)map.get("url"));
        reportInfo.setProjectId(id);
        reportInfoService.save(reportInfo);

        return Result.success(response.getBody());
    }



}

